package com.jd.service.impl;


import com.jd.domain.Product;
import com.jd.domain.Store;
import com.jd.dto.ProductDto;
import com.jd.feign.StoreService;
import com.jd.mapper.ProductMapper;
import com.jd.service.IProductService;
import com.jd.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private StoreService storeService;

    @Override
    public AjaxResult add(ProductDto productDto) {
        try {
            // 保存商品信息
            Product product = new Product();
            product.setName(productDto.getName());
            product.setPrice(productDto.getPrice());
            productMapper.add(product);

            // 保存库存到数据库，feign
            Store store = new Store();
            store.setProductId(product.getId());
            store.setNumber(productDto.getNumber());
            AjaxResult add = storeService.add(store);
            if (!add.isSuccess()){
                return add;
            }
            return new AjaxResult();

        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败啦！");
        }

    }
}
